package com.peza.postman.util;

import java.util.Calendar;
import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

@Component("healthLocker")
public class HealthLocker {

	private static Logger logger = Logger.getLogger(HealthLocker.class);
	
	private static HealthLocker instance;
	
	private static Calendar startTime;
	
	private static long start ;
	
	private static int total;
	
	private static int success;
	
	private static int fail;
	
	private HealthLocker() {
		
	}
	
	@PostConstruct
	private void init(){
		start = System.currentTimeMillis();
		startTime = Calendar.getInstance();
	}
	
	public static HealthLocker getInstance(){
		if (instance == null){
			instance = new HealthLocker();
		}
		return instance;
	}
	
	public String getInfo(){
		StringBuffer output = new StringBuffer();
		
		output.append("PostMan version 1.0 \n");
		output.append("-------------------- \n");
		output.append("Time running : " + getUpTime() + " \n");
		output.append("Total mails processed : " + (fail+success) +" \n");
		output.append("Total success : " + success +" \n");
		output.append("Total fail : " + fail +" \n");
		
		return output.toString();
	}
	
	
	public String getUpTime(){
		long now = System.currentTimeMillis();
		long shift = now - start;
		long days  = TimeUnit.MILLISECONDS.toDays(shift);
		long hours = TimeUnit.MILLISECONDS.toHours(shift - TimeUnit.DAYS.toMillis(days));
		long mins  = TimeUnit.MILLISECONDS.toMinutes(shift - TimeUnit.HOURS.toMillis(hours));
		long secs  = (TimeUnit.MILLISECONDS.toSeconds((shift - TimeUnit.MINUTES.toMillis(mins))));
				
		String output = String.format("%d days, %d hours, %d min, %d sec", days, hours, mins, secs);
		return output;
	}
	
	public void addSuccess(){
		success++;
	}
	
	public void addFail(){
		fail++;
	}
	
	public static void main (String [] args) throws InterruptedException{
		HealthLocker.getInstance().init();
		System.out.println(start);
		Thread.sleep(2500);
		System.out.println(System.currentTimeMillis() - start);
		System.out.println(HealthLocker.getInstance().getUpTime());
	}
	
	
}
